*-------------------------------------------------------------------------------*
* DIRECTORIES
*-------------------------------------------------------------------------------*
local l_direc_data `1'
local l_direc_code `2' 
local l_direc_figtab `3' 

local direc_data_raw  `l_direc_data'/raw
local direc_data_clean `l_direc_data'/clean



*---------------------------------------------------------------------------------*
* TAKEOVER FIG 
*---------------------------------------------------------------------------------*

local l_spec1 reghdfe Y X , noabsorb resid
local l_spec2 reghdfe Y X , a(fe_jm) resid
local l_spec3 reghdfe Y X , a(fe_mt) resid
local l_spec4 reghdfe Y X , a(fe_mt fe_jm) resid

foreach l_s in 1 4 {
	*---------------------------------------------------------------------------------*
	* LINEAR VERSION
	*---------------------------------------------------------------------------------*
	use "`direc_data_clean'/mjt_regready_agg050.dta", clear

	keep if !regexm(journal_name, "(Rest of PubMed)")
	keep if year >= 1985

	gen X = eshr2_frcnt_all
	gen Y = jshr_frcnt_all

	`l_spec3'
	keep if e(sample)
	

	// estimate save parameters and expectations
	`l_spec`l_s''
	predict E_xbd , xbd
	predict E_d, d
	replace E_d = E_d + _b[_cons]
	rename _reghdfe_resid E_xi
	gen E_b = _b[X]

	// collapse to averages at journal+mesh level
	gcollapse (mean) Y X E_xbd E_d E_xi, by(journal_name mesh_tree fe_j fe_m fe_jm E_b)


	// expected market shares (mesh share of journal)
	gen E_shrJournal = Y

	*------------------------------------
	* loop
	*------------------------------------
	gen j = fe_j

	su j
	qui forval l_journal = `r(min)'/`r(max)' {
		
		nois di "`l_journal'"

		// record cf journal shares
		egen tmp = max(E_shrJournal) if fe_j==`l_journal' , by(fe_m)
		egen O_E_shrJournal = max(tmp) , by(fe_m)
		recode O_E_shrJournal .=0
		drop tmp

		// assign cf editor composition
		egen tmp = max(X) if fe_j==`l_journal' , by(fe_m)
		egen CF_deltaEditor = max(tmp) , by(fe_m)
		recode CF_deltaEditor .=0
		drop tmp

		gen ECF_shrJournal = E_shrJournal + (E_b * (CF_deltaEditor - X))

		tabstat E_shrJournal O_E_shrJournal ECF_shrJournal , by(fe_j)

		//similarity calcs: OTHER TO ORIGINAL 
			//    CALCULATE EUCLIDIAN DISTANCE &
			//    INNER PRODUCT
			by j, sort: gen dist = sum((O_E_shrJournal-E_shrJournal)^2)
			by j: replace dist = sqrt(dist[_N])
			by j: gen inner_product = sum(O_E_shrJournal*E_shrJournal)
			by j: replace inner_product = inner_product[_N]

			//    CALCULATE LENGTH OF EACH VECTOR
			foreach i in O_E_ E_ {
				by j: gen length`i' = sum(`i'shrJournal^2)
				by j: replace length`i' = sqrt(length`i'[_N])
			}

			//    CALCULATE ANGLE BETWEEN VECTORS
			gen cos_cfold = inner_product/(lengthO_E_*lengthE_)
			drop dist inner_product length*
			
		//similarity calcs: OTHER TO TAKENOVER
			//    CALCULATE EUCLIDIAN DISTANCE &
			//    INNER PRODUCT
			by j, sort: gen dist = sum((O_E_shrJournal-ECF_shrJournal)^2)
			by j: replace dist = sqrt(dist[_N])
			by j: gen inner_product = sum(O_E_shrJournal*ECF_shrJournal)
			by j: replace inner_product = inner_product[_N]

			//    CALCULATE LENGTH OF EACH VECTOR
			foreach i in O_E_ ECF_ {
				by j: gen length`i' = sum(`i'shrJournal^2)
				by j: replace length`i' = sqrt(length`i'[_N])
			}

			//    CALCULATE ANGLE BETWEEN VECTORS
			gen cos_cfnew = inner_product/(lengthO_E_*lengthECF_)
			drop dist inner_product length*

		//level differences in journal
		gen lev_cos_diff_`l_journal' = (cos_cfnew - cos_cfold)
		tabstat lev_cos_diff_`l_journal' , by(journal_name)

		//percent differences in journal
		gen perc_cos_diff_`l_journal' = (cos_cfnew - cos_cfold) / cos_cfold
		tabstat perc_cos_diff_`l_journal' , by(journal_name)

		//share of angle covered
		gen shr_ang_cov_`l_journal' =  (cos_cfnew - cos_cfold) / (1 - cos_cfold)
		tabstat shr_ang_cov_`l_journal' , by(journal_name)

		//share-scaled of angle covered
		gen shr_ang_covperc_`l_journal' =  ((cos_cfnew - cos_cfold) / (1 - cos_cfold))/cos_cfold
		
		gen cos_cfold_`l_journal' = cos_cfold
		
		drop O_E_shrJournal ECF_shrJournal CF_deltaEditor cos_cfold cos_cfnew

	}

	qui forval j=1/18 {
		foreach l_var of varlist lev_cos_diff_* perc_cos_diff_* shr_ang_cov_* shr_ang_covperc_* cos_cfold_* {
			replace `l_var' = . if regexm("`l_var'", "_`j'$") & j==`j'
		}
	}

	keep j journal_name lev_cos_diff_* perc_cos_diff_* shr_ang_cov_* shr_ang_covperc_* cos_cfold_*
	gduplicates drop
	preserve
		keep j journal_name
		tempfile Tj
		save `Tj', replace
		rename j jtakesover
		rename journal_name journal_nametakesover
		tempfile Tjt
		save `Tjt', replace
	restore
	drop journal_name
	reshape long lev_cos_diff_ perc_cos_diff_ shr_ang_cov_ shr_ang_covperc_ cos_cfold_, i(j) j(jtakesover)
	joinby j using `Tj'
	joinby jtakesover using `Tjt'

	nois di "HERE1: `l_s'"
	gen spec = `l_s'
	nois di "HERE2: `l_s'"
	tempfile TMP`l_s'
	save `TMP`l_s'', replace
	nois di "HERE3: `l_s'"
}
use `TMP1', clear
append using `TMP4'

su cos_cfold_ if spec==1
gen lev_per_sd = lev_cos_diff_/`r(sd)'

//per editor @ mean number of eds = 57
gen shr_ang_cov_pered = shr_ang_cov_ / 57


hist cos_cfold_ if spec==1 ///
	, frac w(0.05) start(0) fc(gs5%75) lc(none) ///
	yscale(r( )) ylab(, angle(horiz) format(%4.2fc) grid gmax gmin) ytitle("Frac. of" "journal" "pairs", orient(hoirz))  ///
	xtitle("Journal pair similarity", margin(small)) xscale(r(-.1 1.05)) xlab(0 .25 .5 .75 1, format(%3.2fc) grid gmax gmin)  ///
	graphregion(color(white) lc(white)) bgcolor(white) ///
	scale(1.25) name(cos_cfold_, replace)
gr export "`l_direc_figtab'/fig_takeovers_presim.pdf", as(pdf) replace	

tw 	(hist lev_cos_diff_ if spec==1, fc(gs10%75) lc(none) frac w(0.05) start(-0.05)) ///
	(hist lev_cos_diff_ if spec==4, fc(none) lc(black) lw(medthick) frac w(0.05) start(-0.05)) ///
	, ///
	legend(order(1 "No Controls" 2 "Missions + Markets Controls") size(small) region(lc(white)) ) ///
	yscale(r( )) ylab(, angle(horiz) format(%3.1fc) grid gmax gmin) ytitle("Frac. of" "journal" "pairs", orient(hoirz))  ///
	xtitle("Level change in pair similarity" "from entire board replacement", margin(small)) xscale(r(-.1 1.05)) xlab(0 .25 .5 .75 1, format(%3.2fc) grid gmax gmin)  ///
	graphregion(color(white) lc(white)) bgcolor(white) ///
	scale(1.25) name(lev_cos_diff_, replace)
gr_edit .yaxis1.title.DragBy 0 -2.14
gr export "`l_direc_figtab'/fig_takeovers_post_levdiff.pdf", as(pdf) replace		

tw 	(hist shr_ang_cov_ if spec==1, fc(gs10%75) lc(none) frac w(0.05) start(-0.05)) ///
	(hist shr_ang_cov_ if spec==4, fc(none) lc(black) lw(medthick) frac w(0.05) start(-0.05)) ///
	if shr_ang_cov_>-0.05,	///
	legend(order(1 "No Controls" 2 "Missions + Markets Controls") size(small) region(lc(white)) ) ///
	yscale(r( )) ylab(, angle(horiz) format(%4.2fc) grid gmax gmin) ytitle("Frac. of" "journal" "pairs", orient(hoirz))  ///
	xtitle("Percent decrease in pair distance" "from entire board replacement", margin(small)) xscale(r(-.05 1.05)) xlab(0 "0%" .25 "25%" .5 "50%" .75 "75%" 1 "100%", grid gmax gmin)  ///
	graphregion(color(white) lc(white)) bgcolor(white) ///
	scale(1.25) name(shr_ang_cov_, replace)
gr_edit .yaxis1.title.DragBy 0 -2.14
gr export "`l_direc_figtab'/fig_takeovers_post_shrang.pdf", as(pdf) replace		




hist shr_ang_cov_pered if spec==4 ///
	, frac w(0.001) start(-0.001) fc(none) lc(black) lw(medthick) ///
	legend(order(1 "Missions + Markets Controls") size(small) region(lc(white)) ) ///
	yscale(r( )) ylab(, angle(horiz) format(%4.2fc) grid gmax gmin) ytitle("Frac. of" "journal" "pairs", orient(hoirz))  ///
	xtitle("Percent decrease in pair distance" "per editor replaced", margin(small)) xlab(0 "0%" .005 "0.5%" .01 "1%" , grid gmax gmin)  ///
	graphregion(color(white) lc(white)) bgcolor(white) ///
	scale(1.25) name(cos_cfold_, replace)
gr_edit .legend.draw_view.setstyle, style(yes)
gr export "`l_direc_figtab'/fig_takeovers_post_shareangpered.pdf", as(pdf) replace	

